﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using Utilities;

namespace BLL
{
    public class ViewManagement
    {
        public static List<StockView> CalculatePercentage(List<StockView> list)
        {
            double total = 0;
            list.ForEach(s=> total = s.Volume+total);
            list.ForEach(s => s.Percentage = (int)(s.Volume / total * 100));

            return list;
        }

        public static List<StockView> CalculateCategoryTotal(List<StockView> list)
        {
            list.ForEach(s => 
            {
                double total = 0;
                list.FindAll(sc => sc.CategoryName == s.CategoryName).ForEach(t => 
                {
                    total = t.Volume + total;
                });
                s.CategoryTotal = total;
            });
            return list;
        }
    }
    public class StockManagement
    {
        /// <summary>
        /// 根据当前库存取得库存变化实例
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static StockChangeRecord GetStockChangeInstance(Stock s)
        {
            var change = new StockChangeRecord()
            {
                Area = s.Area,
                AreaID = s.AreaID,
                Quality = s.Quality,
                QualityID = s.QualityID,
                Category = s.Category,
                CategoryID = s.CategoryID,
                Warehouse = s.Warehouse,
                WarehouseID = s.WarehouseID
            };
            return change;
        }

        public static StockChangeRecord GetStockChangeInstance(int stockid)
        {
            var s = CH.GetDataById<Stock>(stockid);
            return GetStockChangeInstance(s);
        }

        public static bool StocksChange(StockChangeRecord s, out string msg)
        {
            var stock = TryFindStock(s);
            msg = string.Empty;
            var changetype = CH.GetAllData<StockChangeType>(item => item.ID == s.StockChangeTypeID).FirstOrDefault();
            if (changetype.Direction == "入库")
            {
                if (stock != null)//已存在此类库存
                {
                    stock.Volume = stock.Volume + s.Volume;
                    CH.Edit<Stock>(stock);
                }
                else//未能找到此类库存
                {
                    CH.Create<Stock>(new Stock()
                    {
                        WarehouseID = s.WarehouseID,
                        AreaID = s.AreaID,
                        Volume = s.Volume,
                        CategoryID = s.CategoryID,
                        QualityID = s.QualityID,
                        StockName = CH.GetDataById<Category>(s.CategoryID).CategoryName
                    });
                }

            }
            else if (changetype.Direction == "出库")
            {
                if (stock == null)
                {
                    msg = "未能在指定仓库内找到对应库存品";
                    return false;
                }
                else
                {
                    if (s.Volume > stock.Volume)
                    {
                        msg = "找到对应库存品,但是数量小于要求出库的数量，请检查";
                        return false;
                    }
                    else
                    {
                        stock.Volume = stock.Volume - s.Volume;
                        CH.Edit<Stock>(stock);
                    }
                }
            }
            return true;
        }

        public static Stock TryFindStock(StockChangeRecord s)
        {
            var stocks = CH.GetAllData<Stock>(item => item.WarehouseID == s.WarehouseID);
            var t = stocks.FirstOrDefault(item => item.CategoryID == s.CategoryID && item.QualityID == s.QualityID);
            return t;
        }
    }
}